<%--
  Created by IntelliJ IDEA.
  User: JesseChan
  Date: 2022/5/12
  Time: 2:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.Vector" %>
<%@ page import="com.biejiaogroup.librarymanager.entity.Book" %>
<%@ page import="com.biejiaogroup.librarymanager.controller.BookServlet" %>
<%@ page import="com.biejiaogroup.librarymanager.service.BookService" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.biejiaogroup.librarymanager.entity.User" %>
<%@ page import="com.biejiaogroup.librarymanager.service.UserService" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户信息管理</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link href="css/font-awesome.css" rel="stylesheet">

    <link href="css/plugins/footable/footable.core.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
</head>
<body class="gray-bg">
<%--顶部导航条--%>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="index.jsp">图书管理系统</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup"
            aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="topNavBar">
        <div class="navbar-nav">
            <a class="nav-link" href="mainPage.jsp">主页</a>
            <a class="nav-link active" href="userManager.jsp">用户信息管理</a>
            <%
                User u = (User) session.getAttribute("user");
                if (u != null && u.getUserLevel() == 0)
                {
                    out.println("<a class=\"nav-link\" href=\"bookManager.jsp\">图书管理</a>");
                }
            %>
        </div>
    </div>
    <input id="presentUserID" style="visibility: hidden" value="${user.userID}"></input>

    <%
        //登录按钮
        if (session.getAttribute("login") != null)
        {
            out.println("<td>欢迎管理员" + u.getUserName() + "</td>");
            out.println("<div class=\"navbar-form navbar-right\">\n" +
                    "        <button class=\"btn btn-outline-danger my-2 my-sm-0\" type=\"button\" data-toggle=\"modal\" data-target=\"#exitModal\">退出登录</button>\n" +
                    "    </div>");
        }
        else
        {
            out.println("<div class=\"navbar-form navbar-right\">\n" +
                    "        <button class=\"btn btn-outline-success my-2 my-sm-0\" type=\"button\" data-toggle=\"modal\" data-target=\"#loginModal\">Login</button>\n" +
                    "    </div>");
        }
    %>
</nav>


<%--搜索--%>
<div class="wrapper wrapper-content bg-light">
    <div class="ibox-content">
        <form action="queryUser" method="post" id="search-form">
            <div class="input-group">
                <input type="text" placeholder="用户名" name="userName" class="form-control input-lg">
                <div class="input-group-btn">
                    <button class="btn btn-lg btn-primary" type="submit" id="btn_search">搜索</button>
                </div>
            </div>
        </form>
    </div>
</div>
<%--图书管理页面--%>
<div class="row">
    <div class="col-lg-12">
        <div class="ibox">
            <div class="ibox-content">

                <table class="footable table table-stripped toggle-arrow-tiny" data-page-size="15">
                    <thead>
                    <tr>

                        <th data-toggle="true">用户序号</th>
                        <th data-hide="phone">用户名</th>
                        <th data-hide="phone">当前借阅数</th>
                        <th data-hide="phone">用户权限</th>
                        <th class="text-right" data-sort-ignore="true">操作</th>
                    </tr>
                    </thead>

                    <tbody>

                    <%

                        Vector<Book> books = new Vector<Book>();
                        Vector<User> users = new Vector<User>();


                        BookService bookService = new BookService();
                        books = bookService.getAll();


                        UserService userService = new UserService();
                        users = (Vector<User>) session.getAttribute("users");
                        if (users == null) users = userService.getAll();


                        //将books以belongTo为键值存入map
                        Map<Integer, Vector<Book>> m = new HashMap<Integer, Vector<Book>>();
                        for (int i = 0; i < books.size(); i++)
                        {
                            if (m.containsKey(books.get(i).getBelongTo()))
                            {
                                m.get(books.get(i).getBelongTo()).add(books.get(i));
                            }
                            else
                            {
                                Vector<Book> v = new Vector<Book>();
                                v.add(books.get(i));
                                m.put(books.get(i).getBelongTo(), v);
                            }
                        }


                        if (users == null)
                        {
                        }
                        else
                        {
                            for (User user : users)
                            {
                                out.println("<tr>");
                                out.println("<td>" + user.getUserID() + "</td>");
                                out.println("<td>" + user.getUserName() + "</td>");
                                //获取该用户借阅的书籍
                                Vector<Book> books1 = m.get(user.getUserID());
                                if (books1 == null) out.println("<td>0</td>");
                                else out.println("<td>" + books1.size() + "</td>");

                                if (user.getUserLevel() == 1)
                                    out.println("<td><span class=\"label label-primary\">普通用户</span></td>");
                                else out.println("<td><span class=\"label label-danger\">管理员</span></td>");

                                out.println("<td class=\"text-right\">");
                                out.println("<div class=\"btn-group\">");
                                out.println("<a data-toggle=\"modal\" class=\"btn btn-primary\" href=\"#modal-form" + user.getUserID() + "\">查看详情</a>");
                                out.println("</div>");


                                out.println("<div id=\"modal-form" + user.getUserID() + "\" class=\"modal fade\" aria-hidden=\"true\">");
                                out.println("<div class=\"modal-dialog\">");
                                out.println("<div class=\"modal-content\">");
                                out.println("<div class=\"modal-body\">");
                                out.println("<div class=\"row\">");
                                out.println("<div class=\"col-lg-12\">");
                                out.println("<div class=\"ibox\">");
                                out.println("<div class=\"ibox-content\">");
                                out.println("<table class=\"footable table table-stripped toggle-arrow-tiny\" data-page-size=\"15\">");
                                out.println("<thead>");
                                out.println("<tr>");
                                out.println("<th data-toggle=\"true\">图书序号</th>");
                                out.println("<th data-hide=\"phone\">图书名称</th>");
                                out.println("<th data-hide=\"phone\">图书作者</th>");
                                out.println("<th class=\"text-right\" data-sort-ignore=\"true\">操作</th>");
                                out.println("</tr>");
                                out.println("</thead>");

                                //当前user的借阅书籍
                                out.println("<tbody>");

                                Vector<Book> b = m.get(user.getUserID());
                                if (b == null)
                                {

                                }
                                else
                                {
                                    for (Book bb : b)
                                    {
                                        out.println("<tr>");
                                        out.println("<td>" + bb.getBookID() + "</td>");
                                        out.println("<td>" + bb.getBookName() + "</td>");
                                        out.println("<td>" + bb.getBookAuthor() + "</td>");
                                        out.println("<td class=\"text-right\">");
                                        out.println("<div class=\"btn-group\">");
                                        out.println("<a data-toggle=\"modal\" class=\"btn btn-primary\"");
                                        out.println("onclick=\"returnBook(this)\">代归还</a>");
                                        out.println("</div>");
                                        out.println("</td>");
                                        out.println("</tr>");
                                    }
                                }

                                out.println("</tbody>");
                                out.println("<tfoot>");
                                out.println("<tr>");
                                out.println("<td colspan=\"6\">");
                                out.println("<ul class=\"pagination float-right\"></ul>");
                                out.println("</td>");
                                out.println("</tr>");
                                out.println("</tfoot>");
                                out.println("</table>");
                                out.println("</div>");
                                out.println("</div>");
                                out.println("</div>");
                                out.println("</div>");
                                out.println("</div>");
                                out.println("</div>");
                                out.println("</div>");
                                out.println("</div>");


                                out.println("</td>");
                                out.println("<tr>");


                            }
                        }
                    %>
                    </tbody>
                    <tfoot>
                    <tr>
                        <td colspan="6">
                            <ul class="pagination float-right"></ul>
                        </td>
                    </tr>
                    </tfoot>
                </table>

            </div>
        </div>
    </div>

    <div id="exitModal" class="modal fade" aria-hidden="true" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">确定要退出吗？</h4>
                </div>
                <div class="modal-body">
                    <form class="m-t" role="form">
                        <button type="button" class="btn btn-sm btn-danger float-right m-t-n-xs"
                                id="btn_confirm_logout">退出
                        </button>
                    </form>
                </div>

            </div>
        </div>
    </div>
</div>
</body>

<!-- Mainly scripts -->
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script src="js/popper.min.js"></script>
<script type="text/javascript" src="js/bootstrap.bundle.js"></script>
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>

<!-- Custom and plugin javascript -->
<script src="js/inspinia.js"></script>
<script src="js/plugins/pace/pace.min.js"></script>

<!-- FooTable -->
<script src="js/plugins/footable/footable.all.min.js"></script>

<script>
    $(document).ready(function () {

        $('.footable').footable();

    });

    function returnBook(obj) {
        var bookID = $(obj).parent().parent().parent().find('td').eq(0).text();
        var bookName = $(obj).parent().parent().parent().find('td').eq(1).text();
        var bookAuthor = $(obj).parent().parent().parent().find('td').eq(2).text();
        $.ajax({
            url: "returnBook",
            type: "post",
            data: {
                bookID: bookID,
            },
            success: function (data) {
                if (data == "success") {
                    alert("还书成功");
                    window.location.reload();
                } else {
                    alert("还书失败");
                }
            }
        });
    }

    //退出登录
    $("#btn_confirm_logout").click(function () {
        $.ajax({
            url: "logout",
            type: "post",
            success: function (data) {
                if (data == "success") {
                    window.location.href = "index.jsp";
                } else {
                    alert("退出失败");
                }
            }
        });
    });


</script>

</html>
